package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.LoginUser;
import com.jy.adscm.wechat.bean.ShCustomerInfo;
import com.jy.adscm.wechat.bean.WechatUser;
import com.jy.adscm.wechat.bean.WechatUserRole;
import com.jy.adscm.wechat.mapper.mysql.ShCustomerInfoMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserRoleMapper;
import com.jy.adscm.wechat.service.WechatUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-23
 */
@Service
public class WechatUserServiceImpl extends ServiceImpl<WechatUserMapper, WechatUser> implements WechatUserService {
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;
    @Autowired
    WechatUserRoleMapper wechatUserRoleMapper;
    @Autowired
    private RedisOperator redis;
    @Override
    public Boolean insertWechatUser(WechatUser wechatUser) {
        boolean insert = super.insert(wechatUser);
        WechatUserRole wechatUserRole =new WechatUserRole();
        wechatUserRole.setRoleId(1);
        wechatUserRole.setUserId(wechatUser.getId());
        wechatUserRoleMapper.insert(wechatUserRole);
        return insert;
    }



    @Override
    public WechatUser selectByOpenId(String openId) {

        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openId);
         if (AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
             if(AdscmBaseUtils.getInstance().isNotEmpty(wechatUser.getPlasmaStationId(),null)){
                 ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatUser.getPlasmaStationId());
                 if (AdscmBaseUtils.getInstance().isNotEmpty(shCustomerInfo,null)){
                     wechatUser.setPlasmaStationName(shCustomerInfo.getName());
                 }

             }
         }
        return wechatUser;
    }

    @Override
    public List<WechatUser> selectByRoleId(String roleId, String pid) {
        if ("4".equals(roleId)){
            pid=null;
         }
        List<WechatUser> wechatUsers= wechatUserMapper.selectByRoleId(roleId,pid);

        return wechatUsers;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String login(LoginUser loginUser)throws AdscmSystemException {
        //先查询是否存在这个用户
        Wrapper<WechatUser> wrapper=new EntityWrapper<>();
        wrapper.eq("name",loginUser.getName());
        List<WechatUser> wechatUsers = super.selectList(wrapper);
        if ( !(wechatUsers!=null&&wechatUsers.size()>0)){
            throw  new AdscmSystemException("用户不存在,联系管理员！");
        }
        WechatUser wechatUser = wechatUsers.get(0);

        if(!loginUser.getPassword().equals( wechatUser.getPassword())){
            throw  new AdscmSystemException("密码不正确");
        }
       String sessionKey=  UUID.randomUUID().toString().replace("-","");
        redis.set(sessionKey,wechatUser.getOpenid());
        redis.set(wechatUser.getOpenid(),sessionKey);
        return sessionKey;
    }

    @Override
    public Boolean loginout(String sessionKey)throws AdscmSystemException {
        if (AdscmBaseUtils.getInstance().isNotEmpty(sessionKey,null)){
            String openId = redis.get(sessionKey);
            if (AdscmBaseUtils.getInstance().isNotEmpty(openId,null)){
                redis.del(openId);
                redis.del(sessionKey);
            }
        }else {
           throw  new AdscmSystemException("sessionKey 为NULL");
        }

        return true;
    }
}
